const fs = require("fs")

const Router = require("koa-router")
const router = new Router()
const uploadUrl = "http://localhost:3001"



router
    .get("/", async ctx => {
        ctx.type = "text/html"
        ctx.response.body = "<h4> link is ok in upload.js get / </h4>"
    })
    .post("/upload_s", async ctx => {
        const _file = ctx.request.files.file
        // 处理单文件和多文件
        if (_file) {
            let file = Array.isArray(_file) ? _file : [_file]
            let res_arr = file.map(item => {
                let href = ""
                try {
                    let arr = item.path.split("\\")
                    href = uploadUrl + "/" + arr[arr.length - 1]
                } catch (error) {
                    console.log("获取文件名出错 | get name error", error);
                    ctx.response.body = {
                        success: false
                    }
                }
                return {
                    ...item,
                    innerText: item.name,
                    href
                }
            })
            ctx.response.body = {
                success: true,
                result: res_arr
            }
        } else {
            ctx.response.body = {
                success: false
            }
        }
    })

module.exports = router
